package com.yoga.service;

import java.util.Date;

import com.yoga.db.CardDao;
import com.yoga.db.UserCardDao;
import com.yoga.db.UserInfoDao;
import com.yoga.entity.UserCard;
import com.yoga.entity.UserInfo;

/**
 * 用户管理服务，提供增加用户，给用户绑定会员卡的功能
 */
public class UserManageService {

	UserInfoDao userInfoDao = new UserInfoDao();

	CardDao cardDao = new CardDao();

	UserCardDao userCardDao = new UserCardDao();

	/**
	 * 增加新用户
	 * 
	 * @return 返回的是新建的用户ID
	 */
	public int addNewUser(String userName, char gender, Date birthDate, String phone, String address) {
		UserInfo user = new UserInfo(userName, gender, birthDate, phone, address);
		return userInfoDao.addUserWithIdReturn(user);
	}

	/**
	 * 绑定会员卡的功能
	 * 
	 * @param userId
	 *            不使用用户名进行绑卡，而是使用id，保证唯一性
	 * @param type
	 *            会员卡的类型
	 * @return 如果绑定成功，返回true
	 */
	public boolean bindCard(int userId, char type) {
		int res = 0;
		Integer cardId = cardDao.getCardIdOfUser(userId, type);
		if (cardId != null) {
			res = cardDao.updateCardInfo(cardId, type);
		} else {
			CardManageService cardManageService;
			if (type == 'Y')
				cardManageService = new YearCardManageService();
			else if (type == 'T')
				cardManageService = new TimesCardManageService();
			else
				return false;
			cardId = cardManageService.addNewCard();
			UserCard userCard = new UserCard(userId, cardId);
			res = userCardDao.insert(userCard);
		}
		if (res > 0)
			return true;
		return false;
	}

}
